package com.apexedu.sync.service.impl;

import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.ConfigUtil;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.util.LittleTimer;
import com.apexedu.sync.engine.ISyncContext;
import com.apexedu.sync.engine.ISyncEngine;
import com.apexedu.sync.engine.SyncEngineFactory;
import com.apexedu.sync.engine.impl.SyncContext;
import com.apexedu.sync.entity.SyncAppMsg;
import com.apexedu.sync.exception.AuCSyncException;
import com.apexedu.sync.service.ISyncService;
import com.apexedu.util.StringUtil;
import com.apexedu.util.json.JsonUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 同步实现
 * Created by ly on 2015/1/13.
 */
@Transactional
@Service("syncService")
public class SyncServiceImpl implements ISyncService {
    protected Log log = LogFactory.getLog(this.getClass());//日志记录

    @Autowired
    private BaseDao baseDao;

    @Transactional
    @Override
    public String syncSystem(TSysUser user, String appids) throws AuCSyncException {
        log.debug("syncSystem start : ");
        LittleTimer timer = new LittleTimer();
        try {
            String sql = "select * from T_Access_App_Reg a where a.state = 1 and appid not in (?,?)";
            if (StringUtil.isNotBlank(appids)) {
                sql += " and a.appid in ('" + appids.replaceAll(",", "','") + "')";
            }
            List<TAccessAppReg> list = DBUtil.queryAllBeanList(sql, TAccessAppReg.class, ConfigUtil.getString("portalCode"),
                    ConfigUtil.getString("AuCenterAccessEnterCode"));
            List<SyncAppMsg> listMsg = new ArrayList<SyncAppMsg>();
            ISyncEngine engine = SyncEngineFactory.getInstance();
            for (TAccessAppReg app : list) {
                ISyncContext context = SyncContext.build(engine, baseDao, app, user);
                engine.execute(context);
                listMsg.add(context.getMsg());
            }
            return JsonUtil.getJSONString(listMsg);
        } catch (AuCSyncException e) {
            log.error("syncSystem error : " + e);
            throw e;
        } catch (Exception e) {
            log.error("syncSystem error : " + e);
            throw new AuCSyncException(AuCSyncException.UNKNOWN_SERVICE_ERROR, e.getMessage());
        } finally {
            log.debug("syncSystem end : " + timer.end());
        }
    }

    @Override
    public void syncUser() throws AuCSyncException {
        log.debug("syncUser start : ");
        LittleTimer timer = new LittleTimer();
        try {
            DBUtil.callProcQueryObject("pro_inituser(?)");
        } catch (SQLException e) {
            log.error("syncUser error : " + e);
            throw new AuCSyncException(AuCSyncException.UNKNOWN_SERVICE_ERROR, e.getMessage());
        } finally {
            log.debug("syncUser end : " + timer.end());
        }
    }
}
